package xml_tests.XMLParsing;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author Ganesh Tiwari
 */
public class Jdom {
//
//    Document doc;
//
//    public void createXML() {
//        Element catel = new Element("catalog");
//        doc = new Document(catel);
////-----------------------------------
//        Element bel = new Element("book");
//        bel.setAttribute("id", "001");
//        bel.addContent(new Element("isbn").addContent("23-34-42-3"));
//        bel.addContent(new Element("regDate").addContent("1990-05-12"));
//        bel.addContent(new Element("title").addContent("Operating Systems"));
//        bel.addContent(new Element("publisher").addContent("Pearson"));
//        bel.addContent(new Element("price").addContent("400"));
//        Element ael = new Element("authors");
//        bel.addContent(ael);
//        ael.addContent(new Element("author").addContent("Ganesh Tiwari"));
////        bel.addContent(new Element("authors").addContent(new Element("author").addContent("Nutt")));
//        catel.addContent(bel);//one finished
////---------------------------------------- second element
//        bel = new Element("book");
//        bel.setAttribute("id", "002");
//        bel.addContent(new Element("isbn").addContent("24-300-042-3"));
//        bel.addContent(new Element("regDate").addContent("1995-05-12"));
//        bel.addContent(new Element("title").addContent("Distributed Systems"));
//        bel.addContent(new Element("publisher").addContent("Pearson"));
//        bel.addContent(new Element("price").addContent("500"));
//        ael = new Element("authors");
//        bel.addContent(ael);
//        ael.addContent(new Element("author").addContent("Mahesh Poudel"));
//        ael.addContent(new Element("author").addContent("Bikram Adhikari"));
//        ael.addContent(new Element("author").addContent("Ramesh Poudel"));
//        catel.addContent(bel);//one finished
//    }
//
//    public void displayXML() {
//        try {
//            OutputStream outputStream = System.out;
//            XMLOutputter outp = new XMLOutputter();
//            outp.output(doc, outputStream);
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    public void saveXMLtofile(String fileName) {
//        try {
//            //get writer and save to file
//            FileWriter writer = new FileWriter(fileName);
//            XMLOutputter outp = new XMLOutputter();
//            outp.output(doc, writer);
//            writer.close();
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    public void jDomTree(String fil) throws Exception {
//        SAXBuilder builder = new SAXBuilder();
//        Document docu = builder.build(new File(fil));
//        Element el = docu.getRootElement();
//        listChildren(el, 0);
//    }
//
//    public void listChildren(Element curElement, int depth) {
//        printTabs(depth);
//        System.out.print(curElement.getName());
//
//        //check for childrens, if there is no children , print the element's value
//        if(curElement.getChildren().size()==0){
//            System.out.print(" : "+curElement.getValue());
//        }
//        //check for attributes, if there is attribute, print them
//        for(int i=0;i<curElement.getAttributes().size();i++)
//        {
//            List atts=curElement.getAttributes();
//            Attribute att=(Attribute)atts.get(i);
//            System.out.print(att.getName()+" :"+att.getValue());
//        }
//        //recursively check for next level in tree hierarchy
//        System.out.println("");
//        List children = curElement.getChildren();
//        Iterator it = children.iterator();
//        while (it.hasNext()) {
//            Element child = (Element) it.next();
//            listChildren(child, depth + 1);
//        }
//    }
//
//    void printTabs(int dep) {
//        for (int i = 0; i < dep; i++) {
//            System.out.print("\t");
//        }
//    }
}
